package org.sgx.jsutil.client;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
/**
 * test related utilities 
 * @author sg
 *
 */
public class TestUtil {
//	private static TestUtil instance;

//	public static TestUtil getInstance() {
//		if (null == instance) {
//			instance = new TestUtil();
//		}
//		return instance;
//	}

	private String[] words; 
	private Random rand;

	public TestUtil() {
		rand = new Random();
		words = new String[] { "neque", "porro", "quisquam", "est", "qui", "dolorem", "ipsum", "quia", "dolor", "sit", "amet", "consectetur", "adipisci", "velit" };
	}
	
	public String randomWords(int wordCount) {
		StringBuffer sb = new StringBuffer();
		randomWords(sb, wordCount); 
		return sb.toString();
	}

	public void randomWords(StringBuffer sb, int wordCount) {
		for (int i = 0; i < wordCount; i++) {
			sb.append(words[randomBetween(0, words.length-1)]+" ");
		}
	}

	public int randomBetween(int min, int max) {
		Random rand = getRandom();
		int randomNum = rand.nextInt(max - min + 1) + min;
		return randomNum;
	}
//	public Color randomColor() {
//		return new Color(randomBetween(0,  255), randomBetween(0,  255), randomBetween(0,  255)); 
//	}
	
	public Random getRandom() {
		return rand;
	}
	

	int randomLetterMinChar = 97, randomLetterMaxChar = 122; 
	
	public String randomLetters(int letterCount) {
		String s = ""; 
		for (int i = 0; i <letterCount; i++) {
			int c = randomBetween(randomLetterMinChar, randomLetterMaxChar); 
			s+=((char)c); //Character.toChars(c); 
		}
		return s; 		
	}

	public Boolean randomBoolean() {
		return randomBetween(0, 9)<5; 
	}
	
	
	
	//small assertation system for testinassertsFailedDetailedMessageg
	public List<String> assertsFailedOriginalMessage = new LinkedList<String>();
	public List<String> assertsFailedDetailedMessage = new LinkedList<String>();
	public List<String> getAssertsFailedOriginalMessage() {
		return assertsFailedOriginalMessage;
	}
	public void cleanTests() {
		assertsFailedOriginalMessage.clear();
		assertsFailedDetailedMessage.clear(); 
	}
	public  String printAssertsFailed() {
		Iterator<String> i = assertsFailedDetailedMessage.iterator();
		String s=(assertsFailedDetailedMessage.isEmpty()?"Non failed asserts":"Asserts failed: ");
		while(i.hasNext()) {
			s+="\n"+i.next();
		}
		return s;
	}
	public  void assertTrue(boolean b, String assertMsg) {
		if(!b) {
			assertsFailedOriginalMessage.add(assertMsg);
			assertsFailedDetailedMessage.add(assertMsg);; 
		}
	}
	public int assertCount=0;
	public  void assertTrue(boolean b) {
		assertCount++;
		if(!b) {
			assertsFailedOriginalMessage.add(assertCount+"");
			assertsFailedDetailedMessage.add(assertCount+"");
		}
	}

	public void assertEquals(Object fact, Object expected, String msg) {
		assertCount++;
		if(!areEquals(fact, expected)) {
			String name = msg==null ? (" unammed "+assertCount): msg;	
//			System.out.println("name: "+name);
			assertsFailedOriginalMessage.add(name);
			assertsFailedDetailedMessage.add(name+" - "+fact+" is not equals as expected "+expected+". ");
		}
	}
	public void assertEquals(Object fact, Object expected) {
		assertEquals(fact, expected, null); 
	}
	private static boolean areEquals(Object fact, Object expected) {
		if(fact==null&&expected==null)
			return true;
		else {
			if(fact==null||expected==null)
				return false;
			return fact.equals(expected); 
		}
	}
}
